angular.module('home.controller', ['home.service'])
    .controller("HomeCtrl", function ($scope, $http, $rootScope,$ionicActionSheet,$timeout,$location,$cordovaBarcodeScanner,$ionicLoading, $ionicPopup, $location,$state) {

        //进入home页面返回键消失
        //console.log($location.path());
        $('#home .bar .button.back-button, .bar button.back-button').css({display:'none'});
        //进入其他页面返回键显示
        $scope.fanhui=function(e){
            var a =document.getElementById('nav')
            var b = e.target ;
            //console.log(a != b);
            if(a!=b){
                $timeout(function(){
                    $('#home .bar .button.back-button, .bar button.back-button').css({display:'block'});
                },300)
            }
        }



        //接口
        //页面加载,获取菜单权限
        var CCSessionId =localStorage.getItem("CCSessionId");
        console.log(CCSessionId);
        var url = $rootScope.myglobal.serverurl + '/authority/permission-module.json?CCSessionId=' + CCSessionId
        $http({
            method: "get",
            url: url
        })
            .success(function (data) {
                console.log(data);
                angular.forEach(data, function (item, index) {
                    switch (item.moduleName) {
                        //查余额
                        case "即时余额":
                            $scope.accountbalance = "#/tab/accountbalance"
                            $scope.accountbalanceif = true;
                            break;
                        //查明细
                        case "交易明细":
                            $scope.detailaccountChoose = "#/tab/detailaccountChoose"
                            $scope.detailaccountChooseif = true;
                            break;
                        //查状态
                        case "作业状态图":
                            $scope.statequery = "#/tab/statequery"
                            $scope.statequeryif = true;
                            break;
                        // 请款
                        case "申请借款":
                            $scope.sumrequest = "#/tab/fundsquest"
                            $scope.sumrequestif = true;
                            break;
                        // 支付0
                        case "付款":
                            $scope.pay = "#/tab/pay"
                            $scope.payif = true;
                            break;
                        // 审批
                        case "待处理作业":
                            $scope.approve = "#/tab/approve"
                            $scope.approveif = true;
                            break;
                        // 差旅
                        case "交易明细":
                            $scope.accountbalance = "#/tab/accountbalance"
                            $scope.accountbalanceif = true;
                            break;
                        // 商品
                        case "商铺管理":
                            $scope.goods = "#/tab/goods"
                            $scope.goodsif = true;
                            break;
                        // 我的订单
                        case "采购订单":
                            $scope.myorder = "#/tab/myorder"
                            $scope.myorderif = true;
                            break;
                        // 智能赚
                        case "智能配资":
                            $scope.smartgain = "#/tab/smartgain"
                            $scope.smartgainif = true;
                            break;
                        // 资金流
                        case "资金规划总览":
                            $scope.capitalflow = "#/tab/capitalflow"
                            $scope.capitalflowif = true;
                            break;
                        default:
                            break;
                    }
                })
                //判断item.moduleName ，没有的给隐藏掉
                //console.log($scope.accountbalance);
                //查余额
                if ($scope.accountbalance == undefined || $scope.accountbalance == "") {
                    $scope.accountbalanceif = false;
                    //console.log($scope.accountbalanceif);
                }
                //查明细
                if ($scope.detailaccountChoose == undefined || $scope.detailaccountChoose == "") {
                    $scope.detailaccountChooseif = false;
                }
                //查状态
                if ($scope.statequery == undefined || $scope.statequery == "") {
                    $scope.statequeryif = false;
                }
                // 请款
                if ($scope.sumrequest == undefined || $scope.sumrequest == "") {
                    $scope.sumrequestif = false;
                }
                // 支付0
                if ($scope.pay == undefined || $scope.pay == "") {
                    $scope.payif = false;
                }
                // 审批
                if ($scope.approve == undefined || $scope.approve == "") {
                    $scope.approveif = false;
                }
                // 差旅
                //if( $scope.detailaccountChoose == undefined||$scope.detailaccountChoose == ""){
                //    $scope.detailaccountChooseif=false;
                //}
                // 商品
                if ($scope.goods == undefined || $scope.goods == "") {
                    $scope.goodsif = false;
                }
                // 我的订单
                if ($scope.myorder == undefined || $scope.myorder == "") {
                    $scope.myorderif = false;
                }
                // 智能赚
                if ($scope.smartgain == undefined || $scope.smartgain == "") {
                    $scope.smartgainif = false;
                }
                // 资金流
                if ($scope.capitalflow == undefined || $scope.capitalflow == "") {
                    $scope.capitalflowif = false;
                }


            })



        var CCSessionId =localStorage.getItem("CCSessionId");
        var url = $rootScope.myglobal.serverurl + '/authority/userlogin_out/1/edit.json?CCSessionId=' + CCSessionId
        $http({
            method: "get",
            url: url,
            params:{
                userName:"yxf" ,
                userPassword:"123456",
                tag:1,
            }
        })
            .success(function(data){
                console.log(1);
                console.log(data);
            })





//扫一扫功能

        $scope.scanBarcode = function() {
            console.log(1.);
            $cordovaBarcodeScanner.scan().then(function(imageData) {
                $rootScope.imageData = imageData;
                alert(imageData);
                alert(imageData.text);
                alert("Barcode Format -> " + imageData.format);
                alert("Cancelled -> " + imageData.cancelled);
            }, function(error) {
                alert("An error happened -> " + error);
                $rootScope.error=error
            });
        };

//携程跳转
        var CCSessionId = localStorage.getItem("CCSessionId");
        console.log(CCSessionId);
        $scope.open = function () {
            var target = "_blank";
            var options = "location=no,hidden=yes,toolbar=no,hardwareback=yes";
            inAppBrowserRef = cordova.InAppBrowser.open('http://192.168.50.168:8080/JMCC/xiecheng.html?CCSessionId=' + CCSessionId, target, options);

            inAppBrowserRef.addEventListener('loadstart', loadStartCallBack);

            inAppBrowserRef.addEventListener('loadstop', loadStopCallBack);

            inAppBrowserRef.addEventListener('loaderror', loadErrorCallBack);

        }

        //loadstart在In App Browser启动加载页面（仅在open()调用后）调用该事件。
        function loadStartCallBack() {
            $ionicLoading.show({
                template: 'Loading...'
            });
        }
        function loadStopCallBack() {
            $ionicLoading.show({
                template: 'Loading...'
            });
            $timeout(function () {
                $ionicLoading.hide();
            },3000)
            if (inAppBrowserRef != undefined) {
                inAppBrowserRef.show();
            }
        }
        function loadErrorCallBack() {
            $ionicLoading.show({
                template: '连接失败，请退出后重新连接'
            });
            $timeout(function () {
                inAppBrowserRef.close();
            }, 2000);
            inAppBrowserRef = undefined;
        }




    })



